<?php
header("content-type:text/html;charset=utf-8");

include_once "0.1conn.php"; //连接数据库

session_start();

$shop_id = $_SESSION['user']['shop_id'];

$good_id = $_POST['goodid'];
$number = $_POST['number'];
$ans_shop = $_POST['ans_shop'];

//写入缺货记录
$query = "SELECT * FROM lack_record WHERE good_id = '$good_id' AND shop_id = '$shop_id'";
if (mysqli_num_rows(mysqli_query($conn, $query))) {
    $query = "UPDATE lack_record SET lack_num = lack_num + $number WHERE good_id = '$good_id' AND shop_id = '$shop_id'";
    mysqli_query($conn, $query);
} else {
    $query = "INSERT INTO lack_record (good_id,lack_num,shop_id)
            values ('$good_id','$number','$shop_id')";
    mysqli_query($conn, $query);
}

//写入调货申请
$query = "INSERT INTO transfer_record (good_id,num,ask_shop,ans_shop,status) VALUES ('$good_id','$number','$shop_id','$ans_shop','wait')";
mysqli_query($conn, $query);

echo json_encode(array('status' => '已写入缺货记录并发出调货申请'));

mysqli_close($conn);
